package com.zhentao.controller;

import com.zhentao.service.UsersService;
import com.zhentao.pojo.Users;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    UsersService usersService;

    // 用户管理：分页查询
    @GetMapping("/list")
    public Result listUsersByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String username) {
        return usersService.listUsersByPage(page, size, username);
    }


    // 用户管理：添加
    @PostMapping("/add")
    public Result addUser(@RequestBody Users user) {
        return usersService.addUser(user);
    }



    // 用户管理：修改
    @PostMapping("/update")
    public Result updateUser(@RequestBody Users user) {
        return usersService.updateUser(user);
    }


    // 用户管理：删除
    @DeleteMapping("/delete/{userId}")
    public Result deleteUser(@PathVariable Integer userId) {
        return usersService.deleteUser(userId);
    }


    // 分配角色：角色数据回显
    @GetMapping("/roles/{userId}")
    public Result getUserRoles(@PathVariable Integer userId) {
        return usersService.getUserRoles(userId);
    }


    // 分配角色：保存角色数据
    @PostMapping("/assignRoles/{userId}")
    public Result saveUserRoles(@PathVariable Integer userId, @RequestBody List<Integer> roleIds) {
        return usersService.saveUserRoles(userId, roleIds);
    }




}

